package guiWindows;

import java.awt.Cursor;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragGestureRecognizer;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceContext;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DragSourceMotionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import javax.activation.DataHandler;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.ListModel;
import javax.swing.TransferHandler;
import javax.swing.tree.DefaultMutableTreeNode;

public class TreeHandler extends TransferHandler{
	private static final long serialVersionUID = 1L;

    private final Map<Integer, Cursor> dropCursorOverrides = new HashMap();

	DataFlavor localArrayFlavor;

	String localArrayType = DataFlavor.javaJVMLocalObjectMimeType
			+ ";class=java.lang.String";
	
	JTree source;

	//private MyDragGestureRecognizer myRecognizer;

	public TreeHandler() {
		try {
			localArrayFlavor = new DataFlavor(localArrayType);
		} catch (ClassNotFoundException e) {
			System.out.println("ArrayListTransferHandler: unable to create data flavor");
		}
	}

	public boolean importData(JComponent c, Transferable t) {
		JTextArea targetArea = null;
		String stringAux = null;
		if (!canImport(c, t.getTransferDataFlavors())) {
			System.out.println("cant import");
			return false;
		}
		try {
			targetArea = (JTextArea) c;
			if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
				stringAux = ((String) t.getTransferData(DataFlavor.stringFlavor)).toLowerCase();
				if(stringAux.compareTo("\\/")==0 || stringAux.compareTo("/\\")==0 || stringAux.compareTo("->")==0 || stringAux.equals("W") || stringAux.equals("X")){
					targetArea.insert("(  "+stringAux+"  )", targetArea.getCaret().getDot());
				}else if(stringAux.compareTo("[]")==0 || stringAux.compareTo("<>")==0  || stringAux.compareTo("!")==0){
					targetArea.insert(stringAux+"(  "+"  )", targetArea.getCaret().getDot());					
				}else{
				    if(stringAux.contains(":")){
				        stringAux = stringAux.substring(stringAux.indexOf(":")+1, stringAux.length());
				    }
					targetArea.insert(" "+stringAux.toUpperCase()+" ", targetArea.getCaret().getDot());
				}
			} else {
				System.out.println("the import fail");
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} 
		//int index = targetJList.getSelectedIndex();
		return true;
	}

	public boolean canImport(JComponent c, DataFlavor[] flavors) {
		for (int j = 0; j < flavors.length; j++) {
			if (flavors[j].equals(DataFlavor.stringFlavor)) {
				return true;
			}
		}
		System.out.println("cant import");
		return false;
	}

	protected Transferable createTransferable(Object c) {
		System.out.println("createTransferable");
		if (c instanceof JTree) {
			source = (JTree) c;
			DefaultMutableTreeNode mainNode = (DefaultMutableTreeNode)source.getLastSelectedPathComponent();
			String object = mainNode.getUserObject().toString();
			System.out.println("\n ********************* OBject: "+ object);
			return new DataHandler(object, DataFlavor.stringFlavor.getMimeType());
		}else
			if(c instanceof JList){
				JList button = (JList) c;
				String element = (String)button.getModel().getElementAt(button.getSelectedIndex());
				return new DataHandler("( "+element+" )",DataFlavor.stringFlavor.getMimeType());
			}
		return null;
	}

	public int getSourceActions(JComponent c) {
		System.out.println("getSourceActions");
		return TransferHandler.COPY_OR_MOVE;//COPY_OR_MOVE;
	}
}
